#include "weigen26_34.h"
#include "delay.h"
#include "string.h"


void delay_50us(uint8_t tim)
{
  uint16_t time_out = tim * 50;
  delay_us(time_out);
}
 /*
  * @brief  weigen_init初始化函数
  * @param  None
  * @retval None
  */
void weigen_init(void)
{
	GPIO_InitTypeDef GPIO_Inittypedef;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	GPIO_Inittypedef.GPIO_Mode = GPIO_Mode_Out_PP;
	GPIO_Inittypedef.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12;
	GPIO_Inittypedef.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Inittypedef);
	WG0 = 1;
	WG1 = 1;
}

 /*
  * @brief  weigen26_out  韦根26输出
  * @param  
  * @retval None
  */
void weigen26_out(uint8_t *uid)
{
	unsigned char  i = 0;
    unsigned char  check_odd; //奇校验
    unsigned char  odd;     //填到韦根数组的奇校验位
    unsigned char  check_even; //偶校验
    unsigned char  even;	//填到韦根数组的偶校验位
    unsigned char  wg_data[3];
    unsigned char  byte = 0;//韦根字节
    unsigned char  bite = 0; //韦根比特
	uint8_t u[4];
	for(i=0;i<4;i++)
	{
		u[i] = uid[3-i];
	}
	memcpy(wg_data,&u[1],3);
	
	//处理获得偶偶校验的数据
	u[0] = wg_data[0];
	u[1] = wg_data[1]&0xf0;
	u[2] = wg_data[1]&0x0f;
	u[3] = wg_data[2];
	//进行前16位的偶校验	取第1字节第8位右移至个位
	//求u[0]和u[1]
	check_even = WG34_Funtion(u[0])+WG34_Funtion(u[1]);
	
	//取个位数判断奇偶
    ((check_even & 0x01) == 1)?(even = 1) : (even = 0);//1的个数为偶数时为0
	
	//进行后16位的奇校验
	check_odd = WG34_Funtion(u[2])+WG34_Funtion(u[3]);
	((check_odd & 0x01) == 1) ? (odd = 0) : (odd = 1); //1的个数为偶数时为1
	
	if(even)
	{ WG1 = 0;
		delay_50us(2);
		WG1 = 1;
	}
	else
	{
		WG0 = 0;
		delay_50us(2);
		WG0 = 1;
	}
	delay_50us(30);
	
	
	//发送26bite韦根数据
	for(byte = 0;byte<3;byte++)
	{
		for(bite=0;bite<8;bite++)
		{
			if(wg_data[byte]&(0x80>>bite))
			{
				WG1 = 0;
				delay_50us(2);
				WG1 = 1;
			}
			else
			{
				WG0 = 0;
				delay_50us(2);
				WG0 = 1;
			}
			delay_50us(30);
		}
	}
	
	
	if(odd)
	{
		WG1 = 0;
		delay_50us(8);
		WG1 = 1;
	}
	else
	{
		WG0 = 0;
		delay_50us(8);
		WG0 = 1;
	}
	delay_ms(70);//给下一次发送的间隔,半ms
}


/**
 * @brief WG34_OutPut
 * @param  uid: 要发送的uid数据
 * @retval 
 */
//暂时用于发送4字节uid
void WG34_OutPut(uint8_t *uid)
{
    unsigned char  i = 0;
    unsigned char  check_odd; //奇校验
    unsigned char  odd;     //填到韦根数组的奇校验位
    unsigned char  check_even; //偶校验
    unsigned char  even;	//填到韦根数组的偶校验位
    unsigned char  wg_data[4];
    unsigned char  byte = 0;//韦根字节
    unsigned char  bite = 0; //韦根比特
	
	for(i=0;i<4;i++)
	{
		wg_data[i] = uid[3-i];//将uid数据放入到wg_data中
	}
	
	//进行前16位的偶校验	取第1字节第8位右移至个位
	//求wg_data[0]和wg_data[1]
	check_even = WG34_Funtion(wg_data[0])+WG34_Funtion(wg_data[1]);
	//取个位数判断奇偶
    ((check_even & 0x01) == 1) ? (even = 1) : (even = 0);//1的个数为偶数时为0
	
	//进行后16位的奇校验
	check_odd = WG34_Funtion(wg_data[2])+WG34_Funtion(wg_data[3]);
	((check_odd & 0x01) == 1) ? (odd = 0) : (odd = 1); //1的个数为偶数时为0
	
	if(even)
	{
		WG1 = 0;
		delay_50us(2);
		WG1 = 1;
	}
	else
	{
		WG0 = 0;
		delay_50us(2);
		WG0 = 1;
	}
	delay_50us(30);
	
	//发送32bite韦根数据
	for(byte = 0;byte<4;byte++)
	{
		for(bite=0;bite<8;bite++)
		{
			if(wg_data[byte]&(0x80>>bite))
			{
				WG1 = 0;
				delay_50us(2);
				WG1 = 1;
			}
			else
			{
				WG0 = 0;
				delay_50us(2);
				WG0 = 1;
			}
			delay_50us(30);
		}
	}
	
	if(odd)
	{
		WG1 = 0;
		delay_50us(2);
		WG1 = 1;
	}
	else
	{
		WG0 = 0;
		delay_50us(2);
		WG0 = 1;
	}
	delay_ms(70);//给下一次发送的间隔,半ms
}





/**
 * @brief WG34_Funtion功能函数，返回8位数据中1的个数
 * @param  num: 数据
 * @retval 
 */
uint8_t WG34_Funtion(uint8_t num)
{
	uint8_t i,n=0;
	for(i=0;i<8;i++)
	{
		if(num&(0x80>>i))
		{
			n++;
		}
	}
	return n;
}
